﻿using WcfGeebaServer.DBDAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using WcfGeebaServer.ContentManage;
using System.Collections.Generic;
using WcfGeebaServer.Users;
using System.Threading;

namespace TestGeeba
{
    
    
    /// <summary>
    ///This is a test class for SubForumDATest and is intended
    ///to contain all SubForumDATest Unit Tests
    ///</summary>
    [TestClass()]
    public class SubForumDATest
    {

        private SubForumDA target;
        private int sid;

        public SubForumDATest(){
            Data.ClearDB();

            this.target = new SubForumDA();
            this.sid = 0;
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void AddSubForumTest()
        {
            string sf = "subforum_test" + (+sid);
            Assert.IsTrue(target.AddSubForum(new GSubForum(sf)));
            Assert.AreEqual(Data.subForums.Count, 1);

            Assert.IsFalse(target.AddSubForum(new GSubForum(sf)));
            Assert.AreEqual(Data.subForums.Count, 1);
        }

        [TestMethod()]
        public void RemoveSubForumTest()
        {
            string sf = "subforum_test" + (+sid);
            Assert.IsFalse(target.RemoveSubForum(new GSubForum(sf)));

            target.AddSubForum(new GSubForum(sf));
            Assert.IsTrue(target.RemoveSubForum(new GSubForum(sf)));
        }

        [TestMethod()]
        public void GetSubForumTest()
        {
            string sf = "subforum_test" + (+sid);
            Assert.IsNull(target.GetSubForum(sf));

            target.AddSubForum(new GSubForum(sf));
            Assert.IsNotNull(target.GetSubForum(sf));
        }

        [TestMethod()]
        public void GetSubForumsTest()
        {
            string sf = "subforum_test" + (+sid);

            List<GSubForum> forums = target.GetSubForums();
            int before = forums == null ? 0 : forums.Count;

            target.AddSubForum(new GSubForum(sf));
            forums = target.GetSubForums();
            int after = forums == null ? 0 : forums.Count;

            Assert.IsTrue((after-before)==1);
        }

        [TestMethod()]
        public void GetThreadsTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            int before = target.GetThreads(sub).Count;

            (new ThreadDA()).AddThread(new GThread("sub" + (++sid), "con", new Member("admin", "admin", true)), sub);
            int after = target.GetThreads(sub).Count; ;

            Assert.IsTrue((after - before) == 1);
        }

        [TestMethod]
        public void GetModeratorsTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            int before = target.GetModerators(sub).Count;
            MemberDA.getMemberDA().AddMember("usrename" + (sid), "1234");
            target.AddModerator(sub, new Member("usrename" + (sid), "1234", true), new ModeratorInfo(DateTime.Now));
            int after = target.GetModerators(sub).Count;

            Assert.IsTrue((after-before) == 1);

        }

        [TestMethod]
        public void AddModeratorTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            Member mem = new Member("usrename" + (sid), "1234", true);
            ModeratorInfo info = new ModeratorInfo(DateTime.Now);
            MemberDA.getMemberDA().AddMember("usrename" + (sid), "1234");
            Assert.IsTrue(target.AddModerator(sub, mem, info));
            Assert.IsTrue(mem.UserType == UserType.Moderator);
        }

        [TestMethod]
        public void RemoveModeratorTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            Member mem = new Member("usrename" + (sid), "1234", true);
            ModeratorInfo info = new ModeratorInfo(DateTime.Now);
            MemberDA.getMemberDA().AddMember("usrename" + (sid), "1234");
            target.AddModerator(sub, mem, info);
            Assert.IsTrue(target.RemoveModerator(sub, mem));
            Assert.IsTrue(mem.UserType == UserType.Member);
        }

        [TestMethod]
        public void GetMessageCountByMemberTest()
        {
            Member member = new Member("memberForMessageCount", "123", true);
            GForum.GetForum().register("memberForMessageCount", "123");
            //MemberDA.getMemberDA().AddMember("memberForMessageCount", "123");
            string sftopic = "Some Crazy Topic";
            GSubForum sf = new GSubForum(sftopic);
            target.AddSubForum(sf);
            GThread thread = new GThread("1", "2", member);
            int threadID = sf.publish(thread);
            thread = sf.GetThread(threadID);
            GPost post1 = new GPost("2", "3", member, thread);
            thread.addPost(post1);
            Assert.IsTrue(target.GetMessageCountByMember(member, sf) == 2);
            GPost post2 = new GPost("3", "4", member, thread);
            post1.addPost(post2);
            Assert.IsTrue(target.GetMessageCountByMember(member, sf) == 3);
        }

        [TestMethod]
        public void RemoveModeratorsTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            Member mem = new Member("usrename" + (sid), "1234", true);
            ModeratorInfo info = new ModeratorInfo(DateTime.Now);
            MemberDA.getMemberDA().AddMember("usrename" + (sid), "1234");
            target.AddModerator(sub, mem, info);

            Member mem2 = new Member("usrename" + (++sid), "1234", true);
            MemberDA.getMemberDA().AddMember("usrename" + (sid), "1234");
            target.AddModerator(sub, mem2, info);

            Assert.IsTrue(target.RemoveModerators(sub.Topic));
            Assert.IsTrue(mem.UserType == UserType.Member); 
            Assert.IsTrue(mem2.UserType == UserType.Member);
        }

        [TestMethod]
        public void getMessagesCountTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            int count = target.getMessagesCount(sub);
            Member mem = new Member("usrename" + (sid), "1234", true);
            GForum.GetForum().register("usrename" + (sid), "1234");
            sub.publish(new GThread("subj","cont",mem));
            sub.publish(new GThread("subj", "cont", mem));
            sub.publish(new GThread("subj", "cont", mem));

            Assert.IsTrue(target.getMessagesCount(sub) == count + 3);
        }

        [TestMethod]
        public void GetLastMessageTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            int count = target.getMessagesCount(sub);
            Member mem = new Member("usrename" + (sid), "1234", true);
            GForum.GetForum().register("usrename" + (sid), "1234");
            sub.publish(new GThread("subj", "cont", mem));
            Thread.Sleep(60);
            int lastID = sub.publish(new GThread("subj", "cont", mem));

            Assert.IsTrue(target.GetLastMessage(sub, mem).Id == lastID);
        }

        [TestMethod]
        public void getMembersPostedTest()
        {
            string sf = "subforum_test" + (+sid);
            GSubForum sub = new GSubForum(sf);
            target.AddSubForum(sub);

            Member mem = new Member("usrename" + (sid), "1234", true);
            GForum.GetForum().register("usrename" + (sid), "1234");
            sub.publish(new GThread("subj", "cont", mem));

            Member mem2 = new Member("usrename" + (++sid), "1234", true);
            GForum.GetForum().register("usrename" + (sid), "1234");
            sub.publish(new GThread("subj", "cont", mem2));

            Assert.IsTrue(target.getMembersPosted(sub).Contains(mem));
            Assert.IsTrue(target.getMembersPosted(sub).Contains(mem2));


        }
    }
}
